
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Xadrez extends MIDlet implements CommandListener {

  private Tabuleiro tela;

	private Command novo = new Command("Novo", Command.SCREEN, 1);

  private Command saida = new Command("Sair", Command.EXIT, 2);

  public Xadrez() {
    try {
      tela = new Tabuleiro(Display.getDisplay(this));
      tela.addCommand(novo);
      tela.addCommand(saida);
      tela.setCommandListener(this);
//      Jogo jogo = tela.getJogo();
    } catch(Exception e) {
//      errorMsg(e);
    }
  }

  public void startApp() throws MIDletStateChangeException {
    if(tela != null) {
      tela.iniciar();
    }
  }

  public void destroyApp(boolean unconditional) throws MIDletStateChangeException {
    tela = null;
    System.gc();
  }

  public void pauseApp() {
    try {
      destroyApp(false);
      notifyDestroyed();
    } catch (MIDletStateChangeException ex) {
    }
  }

  public void commandAction(Command c, Displayable s) {
    if((c == saida) || (c == Alert.DISMISS_COMMAND)) {
      try {
      	destroyApp(false);
      	notifyDestroyed();
      } catch (MIDletStateChangeException ex) {
      }
    } else {
      if (c == novo) {
        tela.resetaTabuleiro();
      }
    }
  }
/*
  void errorMsg(Exception e) {
    e.printStackTrace();
    if(e.getMessage() == null) {
      errorMsg(e.getClass().getName());
    } else {
      errorMsg(e.getMessage());
    }
  }

  void errorMsg(String msg) {
    Alert errorAlert = new Alert("error", msg, null, AlertType.ERROR);
    errorAlert.setCommandListener(this);
    errorAlert.setTimeout(Alert.FOREVER);
    Display.getDisplay(this).setCurrent(errorAlert);
  }
*/
}
